package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.NewsMapper;
import com.newsystem.pojo.Collection;
import com.newsystem.pojo.Comment;
import com.newsystem.mapper.CommentMapper;
import com.newsystem.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class CommentService extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    //用户添加评论
    public CommonResult<Object> instertComment(String userId,String newsId,String content){
        if(content==null || content.equals("")){
            return CommonResult.failed("内容不能为空");
        }
        Comment comment = new Comment();
        comment.setId(MyUUIDUtils.getUUID());
        comment.setUserId(userId);
        comment.setNewsId(newsId);
        comment.setState(0);
        comment.setContent(content);
        if(commentMapper.insert(comment)>0){
            return CommonResult.success(comment.getId());
        }
        return CommonResult.failed("添加评论失败");
    }


    //用户修改评论
    public CommonResult<Object> updateComment(String userId, String commemtId,String content){
        if(content==null || content.equals("")){
            return CommonResult.failed("评论内容不为空");
        }
        Comment comment = commentMapper.selectById(commemtId);
        if(comment==null || comment.getUserId().equals(userId)){
            return CommonResult.failed("无此评论");
        }
        if(comment.getState()==1){
            return CommonResult.failed("此评论已经被和谐");
        }
        comment.setContent(content);
        if(commentMapper.updateById(comment)>0){
            return CommonResult.success(commemtId);
        }
        return CommonResult.failed("更新评论内容失败");
    }

    //todo 用户删除评论
    public CommonResult<Object> deleteComment(String userId, String commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if(comment == null || !comment.getUserId().equals(userId)){
            return CommonResult.failed("查无此评论");
        }
        if (commentMapper.deleteById(commentId) > 0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除评论失败");
    }

    //用户获得相关新闻的评论内容列表
    public CommonResult<Object> selectCommentList(String newsId) {
        List<Map<String,Object>> maps = commentMapper.selectCommentList(newsId);
        if(maps == null){
            return CommonResult.failed("评论列表获取失败");
        }
        return CommonResult.success(maps);
    }
}
